Ext.namespace('Ext.payCal');
Ext.payCal.payCalHistoryStore = [['0', '历史1'], ['1', '历史2']];
var paymentCalculateStore;
function calculate(payClassId) {
	// 薪资对应月份下拉框
	var payCalHistoryCombox = new Ext.form.ComboBox({
				fieldLabel : '历史记录',
				hiddenName : 'pay_history',
				id : 'payCalHistoryCombox',
				store : new Ext.data.SimpleStore({
							fields : ['key', 'value'],
							data : Ext.payCal.payCalHistoryStore
						}),
				displayField : 'value',
				typeAhead : true,
				valueField : 'key',
				mode : 'local',
				triggerAction : 'all',
				selectOnFocus : true,
				emptyText : '请选择...',
				readOnly : true,
				anchor : '90%'
			});

	var paymentCalculatePanel = new Ext.form.FormPanel({
		frame : true,
		anchor : '90%',
		buttonAlign : 'right',
		region : 'north',
		id : 'employMessage',
		bodyStyle : 'padding:5px 5px 0',
		height : 80,
		items : [{
			layout : 'column',
			items : [{
						columnWidth : .5,
						layout : 'form',
						items : [{
									fieldLabel : '薪资对应年月',
									anchor : '90%',
									name : 'pay_month',
									id : 'pay_month',
									emptyText : '请输入薪资对应年月',
									xtype : 'textfield'
								}]
					}, {
						columnWidth : .5,
						layout : 'form',
						items : [payCalHistoryCombox]
					}],
			buttons : [{
				text : '开始计算',
				id : '_btnSearch',
				handler : function() {
					//验证是否输入月份
					if (Ext.getCmp('pay_month').getValue() == ""){
						alert('请输入需进行薪资计算的月份！');
						return;
					}
					
					Ext.MessageBox.show({
						title : '提示',
						msg : '您确定开始计算吗?',
						buttons : Ext.MessageBox.OKCANCEL,
						fn : function(btn) {
							if (btn == "ok") {
								Ext.Ajax.request({
									url : "payEmpdataAction.action",
									method : "post",
									params : {queryType : "calData",
											'filter.payTypeId' : payClassId,
											'filter.monthStart' : Ext
													.getCmp('pay_month')
													.getValue(),
											'filter.hisId' : Ext
													.getCmp('payCalHistoryCombox')
													.getValue()
									},
									success : function(response) {
										var responseTextVale = Ext.util.JSON.decode(response.responseText);
										if (responseTextVale.runFlag == 0){
											alert("计算出错，请检查员工相应信息或联系管理员！");
											return;
										}else{
											paymentCalculateStore.on('beforeload',
												function() {
													paymentCalculateStore.baseParams = {
														queryType : "payData",
														'filter.payTypeId' : payClassId,
														'filter.monthStart' : Ext
																.getCmp('pay_month')
																.getValue(),
														'filter.hisId' : Ext
																.getCmp('payCalHistoryCombox')
																.getValue()
													}
											});
											paymentCalculateStore.reload();
										}
									},
									failure : function() {
										Ext.Msg.show({
											title : '操作信息',
											msg : '数据处理异常,请联系管理员!',
											buttons : Ext.Msg.OK
										});
									}
								});
							}
							if (btn == "cancel") {
								return;
							}
						}
					});
				}
			}, {
				text : '查看结果',
				id : '_btnSearch',
				handler : function() {
					paymentCalculateStore.on('beforeload', function() {
								paymentCalculateStore.baseParams = {
									queryType : "payData",
									'filter.payTypeId' : payClassId,
									'filter.monthStart' : Ext
											.getCmp('pay_month').getValue(),
									'filter.hisId' : Ext
											.getCmp('payCalHistoryCombox')
											.getValue()
								}
							});
					paymentCalculateStore.reload();
				}
			}]
		}]
	});

	// 获取表头信息及所对应字段信息---------------------------------------------------------------------------------
	var columnModelResponseText;
	var storeFieldResponseText;

	var conn = Ext.lib.Ajax.getConnectionObject().conn;
	conn.open("POST", 'dynamicColumnAction.action', false);
	conn.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("payClassId=" + payClassId + "&queryType=columnModel");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	if (responseTextVale.columnModelList == "") {
		alert("请先设定薪资项目，并至少设定一项的显示状态为显示！");
		return;
	}
	columnModelResponseText = Ext.encode(responseTextVale.columnModelList);
	columnModelResponseText = columnModelResponseText.substr(1,
			columnModelResponseText.length - 2);
	var reg_header = new RegExp("\"header\"", "g");
	var reg_dataIndex = new RegExp("\"dataIndex\"", "g");
	var reg = new RegExp("\"", "g");
	columnModelResponseText = columnModelResponseText.replace(reg_header,
			"header");
	columnModelResponseText = columnModelResponseText.replace(reg_dataIndex,
			"dataIndex");
	columnModelResponseText = columnModelResponseText.replace(reg, "'");

	conn.open("POST", 'dynamicColumnAction.action', false);
	conn.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=UTF-8");
	conn.send("payClassId=" + payClassId + "&queryType=storeField");
	var responseTextVale = Ext.util.JSON.decode(conn.responseText);
	storeFieldResponseText = Ext.encode(responseTextVale.storeFieldList);
	storeFieldResponseText = storeFieldResponseText.substr(1,
			storeFieldResponseText.length - 2);
	var reg_name = new RegExp("\"name\"", "g");
	var reg_mapping = new RegExp("\"mapping\"", "g");
	storeFieldResponseText = storeFieldResponseText.replace(reg_name, "name");
	storeFieldResponseText = storeFieldResponseText.replace(reg_mapping,
			"mapping");
	storeFieldResponseText = storeFieldResponseText.replace(reg, "'");

	// 查询显示的表格
	// var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel(eval("[new Ext.grid.RowNumberer(), "
			+ "{header:'',dataIndex:'payId',hidden:true},"
			+ "{header:'',dataIndex:'his_id',hidden:true},"
			+ "{header:'',dataIndex:'employee_id',hidden:true},"
			+ "{header:'',dataIndex:'pay_class_id',hidden:true},"
			+ "{header:'员工姓名',dataIndex:'name'},"
			+ "{header:'工资月份',dataIndex:'period'}," + columnModelResponseText
			
			+ ",{header:'工资调整',dataIndex:'',anchor:'90%'," 
			+ "renderer:function(v,p,record){return \"<input type='button' class='x-btn' value='结果调整' " 
			+ "onClick=salaryAjust('\"+record.data.pay_class_id+\"','\"+record.data.employee_id+\"','\"+record.data.period+\"')"
			+ ">\";}" 
			+ "}"
				
			+ "]"));
			
	// 数据源
	paymentCalculateStore = new Ext.data.Store({
		url : 'payEmpdataAction.action',
		reader : new Ext.data.JsonReader({
			root : 'payDataList',
			id : 'paymentCalculateStore'
		}, eval("[" + "{name:'payId',mapping:'payId'},"
			+ "{name:'his_id',mapping:'his_id'},"
			+ "{name:'employee_id',mapping:'employee_id'},"
			+ "{name:'pay_class_id',mapping:'pay_class_id'},"
			+ "{name:'name',mapping:'name'},"
			+ "{name:'period',mapping:'period'},"
			+ storeFieldResponseText + "]"))
	});
	// paymentCalculateStore.load();
	
	var paymentCalculateGrid = new Ext.grid.GridPanel({
		footer : true,
		anchor : '90%',
		region : 'center',
		id : 'employeePaymentGrid',
		autoScroll : true,
		bbar : new Ext.PagingToolbar({
			pageSize : pageSize,
			store : paymentCalculateStore,
			displayInfo : true,
			dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
			emptyMsg : '没有记录'
		}),
		// sm : sm,
		cm : cm,
		store : paymentCalculateStore,
		// frame : true,
		loadMask : true,
		loadMask : {
			msg : '正在加载数据...'
		}
	});

	var paymentCalculateWin = new Ext.Window({
				closable : true,
				draggable : true,
				id : 'paymentCalculateWin',
				layout : 'border',
//				region : 'center',
				title : '员工薪资计算',
				width : 800,
				height : 400,
				plain : true,
				items : [paymentCalculatePanel, paymentCalculateGrid]
			});
	paymentCalculateStore.removeAll();
	paymentCalculateWin.show();
}

//薪资结果低于城市最低工资标准补足输入窗口-------------------------------------------------------------------------------
function salaryAjust(payClassId, employeeId, period){
	var salaryAjustPanel = new Ext.form.FormPanel({
		frame : true,
		anchor : '90%',
		buttonAlign : 'center',
		labelWidth : 80,
		id : 'salaryAjustPanel',
		bodyStyle : 'padding:5px 5px 0',
		items : [{
			layout : 'column',
			columnWidth : 1,
			items : [{
						columnWidth : .01,
						layout : 'form',
						items : [{
							name : 'pay_input_id',
							id : 'pay_input_id',
							xtype : 'hidden'
						}]
					},{
						columnWidth : .49,
						layout : 'form',
						items : [{
							fieldLabel : '本月补充金额',
							anchor : '90%',
							name : 'pay_money',
							id : 'pay_money',
							xtype : 'textfield'
						}]
					}, {
						columnWidth : .01,
						layout : 'form',
						items : [{
							name : 'pay_input_id_next',
							id : 'pay_input_id_next',
							xtype : 'hidden'
						}]
					},{
						columnWidth : .49,
						layout : 'form',
						items : [{
							fieldLabel : '下月扣款金额',
							anchor : '90%',
							name : 'pay_money_next',
							id : 'pay_money_next',
							xtype : 'textfield'
						}]
					}],
			buttons : [{
				text : '确定保存',
				id : 'btnSalaryAjust',
				handler : function() {
					Ext.MessageBox.show({
						title : '提示',
						msg : '确定调整结果吗?',
						buttons : Ext.MessageBox.OKCANCEL,
						fn : function(btn) {
							if (btn == "ok") {
								Ext.Ajax.request({
									url : "payEmpdataAction.action?actionType=4",
									method : "post",
									params : {
										'payInput.employee_id' : employeeId,
										'payInput.pay_class_id' : payClassId,
										'payInput.period' : period,
										'payInput.pay_input_id' : Ext.getCmp('pay_input_id').getValue(),
										'payInput.pay_input_id_next' : Ext.getCmp('pay_input_id_next').getValue(),
										'payInput.pay_money' : Ext.getCmp('pay_money').getValue(),
										'payInput.pay_money_next' : Ext.getCmp('pay_money_next').getValue()
									},
									success : function(response) {
										var responseTextVale = Ext.util.JSON.decode(response.responseText);
										if (responseTextVale.runFlag == 2){
											alert("请先在薪资模板中添加相应薪资调整的薪资项目！");
										}else if (responseTextVale.runFlag == 3){
											alert("数据保存成功，但重新计算错误！");
										}else{
											alert("数据保存成功！");
											salaryAjustWin.close();
											paymentCalculateStore.on('beforeload',
												function() {
													paymentCalculateStore.baseParams = {
														queryType : "payData",
														'filter.payTypeId' : payClassId,
														'filter.monthStart' : period
													}
											});
											paymentCalculateStore.reload();
										}
									},
									failure : function() {
										Ext.Msg.show({
											title : '操作信息',
											msg : '数据处理异常,请联系管理员!',
											buttons : Ext.Msg.OK
										});
									}
								});
							}
							if (btn == "cancel") {
								return;
							}
						}
					});
				}
			}, {
				text : '取消关闭',
				id : 'btnAjustWinClose',
				handler : function() {
					salaryAjustWin.close();
				}
			}]
		}]
	});

	var salaryAjustWin = new Ext.Window({
		closable : true,
		draggable : true,
		id : 'salaryAjustWin',
		region : 'center',
		title : '工资低于城市最低工资标准调整',
		width : 400,
		height : 110,
		plain : true,
		items : [salaryAjustPanel]
	});
	salaryAjustWin.show();
	
	Ext.Ajax.request({
		url : "payEmpdataAction.action?actionType=4&queryType=payInput",
		method : "post",
		params : {
			'payInput.employee_id' : employeeId,
			'payInput.pay_class_id' : payClassId,
			'payInput.period' : period
		},
		success: function(response,options){
			var responseTextValue = Ext.util.JSON.decode(response.responseText);
			Ext.getCmp('salaryAjustPanel').getForm().setValues(responseTextValue.payInput);
		}	
	});
	
}
